<?php

require_once dirname(__FILE__).'/../lib/teacher_programGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/teacher_programGeneratorHelper.class.php';

/**
 * teacher_program actions.
 *
 * @package    sf_sandbox
 * @subpackage teacher_program
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class teacher_programActions extends autoTeacher_programActions
{
    public function executeBatchRecordDelete($program_id, $teacher_id){
        $link_obj = LinkTeacherProgramTable::getInstance()->getAssignmentRecord($program_id, $teacher_id);
        $link_obj->delete();
    }

    public function executeBatchApprove($program_id, $teacher_id){
        $link_obj = LinkTeacherProgramTable::getInstance()->getAssignmentRecord($program_id, $teacher_id);
        $this->changeAssignmentStatus($link_obj, LinkTeacherProgramTable::STATUS_APPROVED);
    }
    
    public function executeBatchReject($program_id, $teacher_id){
        $link_obj = LinkTeacherProgramTable::getInstance()->getAssignmentRecord($program_id, $teacher_id);
        $this->changeAssignmentStatus($link_obj, LinkTeacherProgramTable::STATUS_REJECTED);        
    }
    
    public function executeApprove(sfWebRequest $request){
        $program_id = $this->getRequestParameter('program_id');
        $teacher_id = $this->getRequestParameter('teacher_id');
        
        $link_obj = LinkTeacherProgramTable::getInstance()->find(array($program_id, $teacher_id));
        $this->forward404Unless($link_obj);
        
        $this->changeAssignmentStatus($link_obj, LinkTeacherProgramTable::STATUS_APPROVED);
        
        $this->getUser()->setFlash('notice', I18nUtilities::__('Assignment has been approved'));        
        $this->redirect('teacher_program/index');
    }
        
    public function executeReject(sfWebRequest $request){
        $program_id = $this->getRequestParameter('program_id');
        $teacher_id = $this->getRequestParameter('teacher_id');
        
        $link_obj = LinkTeacherProgramTable::getInstance()->find(array($program_id, $teacher_id));
        $this->forward404Unless($link_obj);
        
        $this->changeAssignmentStatus($link_obj, LinkTeacherProgramTable::STATUS_REJECTED);
        $this->getUser()->setFlash('notice', I18nUtilities::__('Assignment has been rejected'));
        $this->redirect('teacher_program/index');        
    }
    
    protected function changeAssignmentStatus(LinkTeacherProgram $link_obj, $status){
        $link_obj->setStatus($status);
        //Previously send SMS if approved, moved to program actions
    }
    
    public function executeMyFilter(sfWebRequest $request){
        $this->forward('teacher_program', 'filter');
    }

    protected function buildQuery()
    {
       $q = parent::buildQuery();
       if(($filled_filter = $this->getRequest()->getParameter('filled_filter')) !== null){
           $this->getUser()->setAttribute('teacher_program_filled_filter',$filled_filter);
       }
       
       if(!is_null($this->getUser()->getAttribute('teacher_program_filled_filter'))){
           $filled = $this->getUser()->getAttribute('teacher_program_filled_filter');
           
           $filled_arr = ProgramTable::getInstance()->getFilledProgramsQuery($filled)->fetchArray();
           $tmp_program_ids = array();
           foreach($filled_arr as $row){
               $tmp_program_ids[] = $row['program_id'];
           }
           if(count($tmp_program_ids)){
               $q->whereIn('program_id', $tmp_program_ids);
           }else{
               $q->whereIn('1<>1');
           }
       }
       
       
      if($this->getUser()->hasCredential('SUPERVISOR_ADMIN')){ // filter of editor user sector
       /*     $sector_id = $this->getUser()->getGuardUser()->getSectorId();
            
            if(!empty($sector_id)){
                $q->innerJoin('r.Teacher teach');
                $q->innerJoin('teach.School sch');
                $q->addWhere('sch.sector_intg_id = ?', $sector_id);   
            }
*/
      }
      
      return $q;
    }
    
  public function executeFilter(sfWebRequest $request)
  {
    $this->setPage(1);

    if ($request->hasParameter('_reset'))
    {
      $this->setFilters($this->configuration->getFilterDefaults());
      $this->getUser()->setAttribute('teacher_program_filled_filter',null);
      $this->redirect('@link_teacher_program');
    }

    $this->filters = $this->configuration->getFilterForm($this->getFilters());

    $this->filters->bind($request->getParameter($this->filters->getName()));
    if ($this->filters->isValid())
    {
      $this->setFilters($this->filters->getValues());

      $this->redirect('@link_teacher_program');
    }

    $this->pager = $this->getPager();
    $this->sort = $this->getSort();

    $this->setTemplate('index');
  }    
  
  public function executeBatch(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    if (!$ids = $request->getParameter('ids'))
    {
      $this->getUser()->setFlash('error', 'You must at least select one item.');

      $this->redirect('@link_teacher_program');
    }

    if (!$action = $request->getParameter('batch_action'))
    {
      $this->getUser()->setFlash('error', 'You must select an action to execute on the selected items.');

      $this->redirect('@link_teacher_program');
    }

    if (!method_exists($this, $method = 'execute'.ucfirst($action)))
    {
      throw new InvalidArgumentException(sprintf('You must create a "%s" method for action "%s"', $method, $action));
    }

    if (!$this->getUser()->hasCredential($this->configuration->getCredentials($action)))
    {
      $this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));
    }

    
    try
    {
      // validate ids
      foreach($ids as $id_comp){
          list($program_id, $teacher_id) = explode('-',$id_comp);
          $link_obj = LinkTeacherProgramTable::getInstance()->getAssignmentRecord($program_id, $teacher_id);
          if(!$link_obj){
              throw new Exception('error while processing data');
          }
		  
		    // execute batch
			$this->$method($program_id, $teacher_id);		  
      }
    }
    catch (sfValidatorError $e)
    {
      $this->getUser()->setFlash('error', 'A problem occurs when processing the data '.$e->getMessage());
    }

    $this->redirect('@link_teacher_program');
  }    
}
